package com.mavenq.fly.business.flow.task;


import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class SelfRepairService {

    //@Bean
    @ExternalTaskSubscription(topicName = "self-repair", processDefinitionKeyIn = {"Process_external"}, lockDuration = 500000)
    public ExternalTaskHandler doSelfRepair(){
        return (externalTask, externalTaskService)->{
            System.out.println("外部任务进入");
            try {
                // 模拟业务处理
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            boolean isFree = (boolean)externalTask.getVariable("isFree");
            if(isFree){
                System.out.println("免费维修");
                // 最后重试次数与重试间隔ms
                externalTaskService.handleFailure(externalTask, "维修是免费的，我不行自修了", "这里可以打印异常", 1, 10000);
            } else {
                System.out.println("收费维修");
                externalTaskService.complete(externalTask);
            }
        };
    }
}
